package com.sx.pipeline.ui.ledger.choose

import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.annotation.NonNull
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.sx.mvvmlibs.bus.RxBus
import com.sx.mvvmlibs.view.SpaceItemDecoration
import com.sx.mvvmlibs.view.SpaceItemDecoration2
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.adapter.LedgerDeviceAdapter
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.app.event.ChooseDeviceEvent
import com.sx.pipeline.app.event.DeviceEvent
import com.sx.pipeline.app.event.RefreshEvent
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.databinding.ActChooseDeviceBinding
import com.sx.pipeline.databinding.ActLedgerBinding
import com.sx.pipeline.repos.http.res.DeviceInfoVo
import com.sx.pipeline.ui.ledger.LedgerVM
import io.reactivex.rxjava3.schedulers.Schedulers


/**
 * @Author: YY
 * @Date: 2022/4/5
 * @Description: 选择设备
 */
@Route(path = RoutePath.Mine.CHOOSE_DEVICE)
class ChooseDeivceActivity :
    CYDataBindingBaseActivity<ActChooseDeviceBinding, LedgerVM>(R.layout.act_choose_device), View.OnClickListener {
    private lateinit var mAdapter: LedgerDeviceAdapter
    private var key=""

    override fun initData() {
        mBinding.setVariable(BR.onClick, this)
        mBinding.titleBar.leftView.setOnClickListener {
            finish()
        }
        mBinding.titleBar.rightView.setOnClickListener {
            RouteManager.Mine.intentToDeviceAdd(1,0)
        }

        //请求设备列表
        mViewModel.getEquipmentList(key)
    }

    private fun initRv() {
        if (!this::mAdapter.isInitialized) {
            mAdapter = LedgerDeviceAdapter()
            mBinding.recyclerView.layoutManager =
                GridLayoutManager(this, 2)
            mBinding.recyclerView.addItemDecoration(
                SpaceItemDecoration2(20)
            )
        }
        mAdapter.setOnItemClickListener { adapter, view, position ->
            var info = adapter.data[position] as DeviceInfoVo
            RxBus.getDefault().post(ChooseDeviceEvent(info.id,info.name))
            finish()
        }
        mBinding.recyclerView.adapter = mAdapter
    }


    override fun initViewObservable() {
        super.initViewObservable()

        mViewModel.deviceListEvent.observe(this) {
            initRv()
            val taskList = it as List<DeviceInfoVo>
            if (mViewModel.page == 1) {
                if (taskList.isNotEmpty()) {
                    mAdapter?.data = taskList as MutableList<DeviceInfoVo>
                    mBinding.recyclerView.visibility = View.VISIBLE
                    mBinding.emptyView.visibility = View.GONE
                } else {
                    mBinding.recyclerView.visibility = View.GONE
                    mBinding.emptyView.visibility = View.VISIBLE
                }
                mBinding.refreshLayout.finishRefresh()
            } else {
                if (taskList.isNotEmpty()) {
                    mAdapter?.addData(taskList)
                    mBinding.refreshLayout.finishLoadMore()
                } else {
                    mBinding.refreshLayout.finishLoadMoreWithNoMoreData()
                }
            }
        }

    }

    override fun initListener() {
        super.initListener()
        mBinding?.refreshLayout?.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(@NonNull refreshLayout: RefreshLayout) {
                mViewModel.page += 1
                mViewModel.getEquipmentList(key)
            }

            override fun onRefresh(@NonNull refreshLayout: RefreshLayout) {
                mViewModel.page = 1
                mViewModel.getEquipmentList(key)
            }
        })
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            //添加设备
            R.id.tv_add ->RouteManager.Mine.intentToDeviceAdd(1,0)

        }
    }

}